﻿
using System.Collections.Generic;

using Irony.Compiler;

namespace IronLua.Ast {

	public class ReturnStatement : Statement {

		readonly ExpressionList expressions = new ExpressionList ();

		public ExpressionList Expressions {
			get { return expressions; }
		}

		public override LuaNodeType NodeType {
			get { return LuaNodeType.ReturnStatement; }
		}

		public ReturnStatement (CompilerContext context, NonTerminal nonTerminal, SourceLocation location, AstNodeList childNodes)
			: base (context, nonTerminal, location, childNodes)
		{
			if (childNodes [1] != null)
				expressions.AddRange (CollectExpressions ());
		}

		IEnumerable<Expression> CollectExpressions ()
		{
			var list = ChildNodes [1];
			foreach (var node in list.ChildNodes [0].ChildNodes)
				yield return node.ChildNodes [0].ToExpression ();

			if (list.ChildNodes.Count > 1 && list.ChildNodes [1] != null)
				yield return list.ChildNodes [1].ToExpression ();
		}

		public override void Accept (ILuaAstVisitor visitor)
		{
			visitor.VisitReturnStatement (this);
		}
	}
}
